package com.systemframework.sys.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.systemframework.annotation.LicenseAnnotation;
import com.systemframework.annotation.LogAnnotation;
import com.systemframework.annotation.OperateAnnotation;
import com.systemframework.common.BaseCommonInterface;
import com.systemframework.enums.BusinessType;
import com.systemframework.sys.dto.request.SysDictRequestDTO;
import com.systemframework.sys.dto.response.SysDictResponseDTO;
import com.systemframework.sys.entity.SysDict;
import com.systemframework.sys.service.SysDictService;
import com.systemframework.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author summer
 * @date 2023-04-04 16:52
 */
@RestController
@RequestMapping(value = "/dict")
public class SysDictController   implements BaseCommonInterface<SysDictRequestDTO, SysDictResponseDTO> {

    private SysDictService sysDictService;

    @Autowired
    public void setSysDictService(SysDictService sysDictService) {
        this.sysDictService = sysDictService;
    }

    @LicenseAnnotation
    @OperateAnnotation(businessType = BusinessType.INSERT,name = "数据字典")
    @Override
    public Result<String> save(@Valid SysDictRequestDTO sysDictRequestDTO) {
        SysDict sysDict = Convert.convert(SysDict.class, sysDictRequestDTO);
        sysDictService.save(sysDict);
        return Result.successResult();
    }

    @LicenseAnnotation
    @OperateAnnotation(businessType = BusinessType.UPDATE,name = "数据字典")
    @Override
    public Result<String> update(SysDictRequestDTO sysDictRequestDTO) {
        SysDict sysDict = Convert.convert(SysDict.class, sysDictRequestDTO);
        sysDictService.updateById(sysDict);
        return Result.successResult();
    }

    @LicenseAnnotation
    @GetMapping(value = "/findLists")
    public Result<List<SysDictResponseDTO>> findLists(){
        List<SysDictResponseDTO> list = Convert.convert(new TypeReference<List<SysDictResponseDTO>>() {
        }, sysDictService.list());
        return Result.successResult(list);
    }

    @LicenseAnnotation
    @OperateAnnotation(businessType = BusinessType.DELETE,name = "数据字典")
    @Override
    public Result<String> del(Integer id) {
        sysDictService.removeById(id);
        return Result.successResult();
    }

    @LicenseAnnotation
    @Override
    public Result<Page<SysDictResponseDTO>> lists(SysDictRequestDTO sysDictRequestDTO) {
        Page<SysDictResponseDTO> page=sysDictService.lists(sysDictRequestDTO);
        return Result.successResult(page);
    }

    @LicenseAnnotation
    @Override
    public Result<SysDictResponseDTO> findById(Integer id) {
        return Result.successResult(Convert.convert(SysDictResponseDTO.class,sysDictService.getById(id)));
    }

    @LicenseAnnotation
    @OperateAnnotation(businessType = BusinessType.BATCHDELETE,name = "数据字典")
    @Override
    public Result<String> batchDel(List<SysDictResponseDTO> lists) {
        List<Integer> ids = lists.stream().map(SysDictResponseDTO::getId).collect(Collectors.toList());
        if(ids.size()>0 && CollUtil.isNotEmpty(ids)){
            sysDictService.removeByIds(ids);
        }
        return Result.successResult();
    }


}
